package com.tengju.data.domain.model.brand;

import com.tengju.data.domain.model.clickhouse.BrandId;
import com.tengju.data.domain.model.clickhouse.CrmRankCrowdDatePerspective;
import com.tengju.data.domain.model.clickhouse.CrmRankCrowdPerspective;
import com.tengju.data.domain.model.clickhouse.dashboard.MonthQuarterEnum;
import com.tengju.data.domain.model.user.DominationLevelEnum;
import com.tengju.data.domain.model.user.IsDirect;
import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.data.domain.shared.DateUtil;
import com.tengju.data.domain.shared.DomainException;
import com.tengju.data.domain.shared.DomainExceptionCode;
import com.tengju.user.domain.model.club.ClubId;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class BrandSaleRankFormFactory {

    public static BrandSaleRankQueryForm create(List<UserIdCode> genSecIdCodes,Long brandId,Integer monthQuarter, Integer crmRankCrowdPerspective,
                                                UserIdCode dominationIdCode, DominationLevelEnum dominationLevel, Long idCode, BigDecimal saleMoneyFrom,
                                                BigDecimal saleMoneyTo, boolean isDirect, String sortColumn, String sortType) {

        BrandSaleRankQueryForm brandSaleRankQueryForm = new BrandSaleRankQueryForm();

        MonthQuarterEnum monthQuarterEnum = MonthQuarterEnum.getMonthQuarter(monthQuarter);
        CrmRankCrowdPerspective crmRankCrowdPerspectiveEnum = CrmRankCrowdPerspective.parseByVal(crmRankCrowdPerspective);

        CrmRankCrowdDatePerspective crmRankCrowdDatePerspective =
                CrmRankCrowdDatePerspective.parseByRankAndDateRange(crmRankCrowdPerspectiveEnum, monthQuarterEnum);

        if (crmRankCrowdDatePerspective == null) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA,"人群视角或时间类型为空");
        }

        Integer month = crmRankCrowdDatePerspective.getMonth();
        Integer compareMonth = crmRankCrowdDatePerspective.getCompareMonth();
        Integer quarter = crmRankCrowdDatePerspective.getQuarter();
        Integer compareQuarter = crmRankCrowdDatePerspective.getCompareQuarter();

        if (month == null && quarter == null) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA,"时间范围为空");
        }

        List<UserIdCode> idCodes = null;
        if (CrmRankCrowdPerspective.KA == crmRankCrowdPerspectiveEnum) {
            idCodes = genSecIdCodes;
        } else if (idCode != null && idCode > 0) {
            idCodes = List.of(new UserIdCode(idCode));
        }

        BrandAnalysisSortType brandAnalysisSortType =
                BrandAnalysisSortType.fromSortColumnAndType(sortColumn, sortType);

        brandSaleRankQueryForm.setCrmRankCrowdDatePerspective(crmRankCrowdDatePerspective);
        brandSaleRankQueryForm.setDominationIdCode(dominationIdCode);
        brandSaleRankQueryForm.setDominationLevel(dominationLevel);
        brandSaleRankQueryForm.setIdCodes(idCodes);
        brandSaleRankQueryForm.setSaleMoneyFrom(Optional.ofNullable(saleMoneyFrom).orElse(null));
        brandSaleRankQueryForm.setSaleMoneyTo(Optional.ofNullable(saleMoneyTo).orElse(null));
        brandSaleRankQueryForm.setIsDirect(Optional.of(isDirect).map(IsDirect::parseByBooleanVal).orElse(null));
        brandSaleRankQueryForm.setMonth(month);
        brandSaleRankQueryForm.setCompareMonth(compareMonth);
        brandSaleRankQueryForm.setBrandId(new BrandId(brandId));
        brandSaleRankQueryForm.setBrandAnalysisSortType(brandAnalysisSortType);
        brandSaleRankQueryForm.setQuarter(quarter);
        brandSaleRankQueryForm.setCompareQuarter(compareQuarter);

        return brandSaleRankQueryForm;


    }



}
